package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.device.domain.CabinetType;
import com.share.device.service.CabinetTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CabinetTypeController
 * @Description 描述
 * @Author sls
 * @Date 2025/6/24 20:16
 */
@Tag(name = "柜机类型接口管理")
@RestController
@RequestMapping("/cabinetType")
public class CabinetTypeController extends BaseController {

    @Autowired
    private CabinetTypeService cabinetTypeService;

    /**
     * 查询柜机类型列表
     */
    @Operation(summary = "查询柜机类型列表")
    @GetMapping("/list")
    public TableDataInfo list(CabinetType cabinetType)
    {
        startPage();
        List<CabinetType> list = cabinetTypeService.selectCabinetTypeList(cabinetType);
        return getDataTable(list);
    }

    @Operation(summary = "获取柜机类型详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cabinetTypeService.getById(id));
    }

    @Operation(summary = "新增柜机类型")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated CabinetType cabinetType)
    {
        return toAjax(cabinetTypeService.save(cabinetType));
    }

    @Operation(summary = "修改柜机类型")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated CabinetType cabinetType)
    {
        return toAjax(cabinetTypeService.updateById(cabinetType));
    }

//    @Operation(summary = "删除柜机类型")
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        boolean b = cabinetTypeService.removeBatchByIds(Arrays.asList(ids));
//        return toAjax(b);
//    }

    @Operation(summary = "删除相机类型")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {  // 改为 String[] 接收
        try {
            // 将 String[] 转为 List<Long>
            List<Long> idList = Arrays.stream(ids)
                    .map(Long::parseLong)
                    .collect(Collectors.toList());

            boolean b = cabinetTypeService.removeBatchByIds(idList);
            return toAjax(b);
        } catch (NumberFormatException e) {
            return AjaxResult.error("ID 格式错误");
        }
    }

    @Operation(summary = "查询全部柜机类型列表")
    @GetMapping("/getCabinetTypeList")
    public AjaxResult getCabinetTypeList()
    {
        return success(cabinetTypeService.list());
    }


}
